/*--------------------------------------------------------------------------------
	DESCRIPTION: Cleaning Arabbarometer

--------------------------------------------------------------------------------*/

*Arab Barometer 1

clear
use "$data_dir/raw/Arab Barometer/ABI_English.dta"

rename country country_ab
decode country_ab, gen(country)
replace country = proper(country) 
replace country = "Palestine, State of" if country=="Palestine"

merge m:1 country using "$data_dir/raw/countrycodes.dta"
drop if _merge==2
drop _merge

gen survey = "Arabarometer"
gen wave = 1

gen weight = .

**********************************
*************TRUST VALUES*********
**********************************

gen trust_govt = .

gen trust_justice = 1 if q2012==1|q2012==2
replace trust_justice = 0 if q2012==3|q2012==4

gen trust_police = 1 if q2014==1|q2014==2
replace trust_police = 0 if q2014==3|q2014==4

gen trust_civil_service = .

gen trust_local_govt = .

gen trust_parties = 1 if q2015==1|q2015==2
replace trust_parties = 0 if q2015==3|q2015==4

gen trust_parliament = 1 if q2013==1|q2013==2
replace trust_parliament = 0 if q2013==3|q2013==4

gen trust_others = 1 if q204==1
replace trust_others = 0 if q204==2

gen trust_military = .

pca trust_justice trust_police trust_parties trust_parliament, comp(1)
predict mtrust_state
summ mtrust_state, de
gen trust_state = 1 if mtrust_state>=r(p50) & mtrust_state!=.
replace trust_state = 0 if mtrust_state<r(p50) & mtrust_state!=.

************************************************
*************INDIVIDUAL CHARACTERISTICS*********
************************************************

gen survey_year =.
replace survey_year = 2006 if ccode=="DZA"
replace survey_year = 2009 if ccode=="BHR"
replace survey_year = 2006 if ccode=="JOR"
replace survey_year = 2006 if ccode=="LBN"
replace survey_year = 2007 if ccode=="MAR"
replace survey_year = 2006 if ccode=="PSE"
replace survey_year = 2007 if ccode=="YEM"

clonevar age = q701
replace age = . if q701==.

gen yob = survey_year-age

gen cohort = ""
forvalues i = 1870(10)2010 {
	replace cohort = "`i's" if yob>=`i' & yob<(`i'+10)
}

*sex
*			1   Male               
*			2   Female                                
*		  -99   Missing

gen sex = 1 if q702==1
replace sex = 2 if q702==2
replace sex = -99 if q702==.|q702==99

*religion
*			1   Do not belong to a denomination
*			2   Buddhist                
*			3   Jewish                  
*			4   Christian               
*			5   Muslim                  
*			6   Other 
*			7   Hindu 
*		  -99   Missing 

gen religion = .
replace religion = 5 if q711==1|q711==3|q711==4
replace religion = 4 if q711==2
replace religion = 6 if q711==5
replace religion = -99 if q711==97|q711==100|q711==.

*edu
*			1   Primary
*			2   Secondary
*			3   Tertiary
*			4   Other
*		  -99   Missing

gen edu = .
replace edu = 1 if q703==2|q703==3
replace edu = 2 if q703==4
replace edu = 3 if q703==5|q703==6|q703==7
replace edu = 4 if edu==1
replace edu = -99 if q703==97|q703==.

*occupation
*			1   Managers
*			2   Professionals
*			3   Technicians and Associate Professionals
*			4   Clerical Support Workers
*			5   Service and Sales Workers
*			6   Skilled Agricultural, Forestry and Fishery Workers
*			7   Craft and Related Trades Workers
*			8   Plant and Machine Operators, and Assemblers
*			9   Elementary Occupations
*			0   Armed Forces Occupations
*	      -99   Missing

gen occupation = .
replace occupation = 1 if inlist(q707, 1, 2)
replace occupation = 2 if inlist(q707, 3, 6)
replace occupation = 3 if inlist(q707, 4)
replace occupation = 4 if inlist(q707, 5)
replace occupation = 6 if inlist(q707, 10)
replace occupation = 7 if inlist(q707, 7, 8)
replace occupation = 8 if inlist(q707, 9)
replace occupation = 9 if inlist(q707, 11)
replace occupation = 0 if inlist(q707, 12)
replace occupation = -99 if inlist(q707, 13, 14, 94, 97, 98, 99)
replace occupation = -99 if q707==.

*employment_status
*			1   Employed
*			2   Unemployed
*			3   Other
*		  -99   Missing

gen employment_status = .
replace employment_status = 1 if q704==1
replace employment_status = 2 if q704==2
replace employment_status = -99 if q704==.|q704==97

*income
*			1   Band 1
*			2   Band 2
*			3   Band 3
*			4   Band 4
*			5   Band 5
*			6   Band 6
*			7   Band 7
*			8   Band 8
*			9   Band 9
*		   10   Band 10
*		  -99   Missing

clonevar income = q716incomedeciles
replace income = 6 if q716incomedeciles==5.5
replace income = 8 if q716incomedeciles==7.5
replace income = -99 if q716incomedeciles==.

*ideology
*			1   left  
*			2         
*			3         
*			4         
*			5         
*			6         
*			7         
*			8         
*			9         
*			10  right
*		   -99  Missing

gen ideology = -99

*marital
*			1   Married
*			2   Not Married
*		  -99   Missing

gen marital = 1 if q709==2 
replace marital = 2 if q709==1|q709==3
replace marital = -99 if q709==97|q709==99

*urban
*			1   Urban
*			2   Rural
*		  -99   Missing

gen urban = -99

*born_country
*			1   Born in country
*			0   Not born in country

gen born_country = .

keep S009 ccode country numcode country_short cow wave trust_state trust_govt trust_justice trust_police trust_civil_service trust_local_govt trust_parties trust_parliament trust_others trust_military  survey_year age yob cohort sex religion edu occupation employment_status income ideology marital urban survey born_country weight 

sort ccode wave survey_year

save "$data_dir/clean/arab1_clean.dta", replace

***************************************************************************************************************

*Arab 2

clear
use "$data_dir/raw/Arab Barometer/ABII_English.dta"

rename country country_ab
decode country_ab, gen(country)

replace country = "Algeria" if country=="1. Algeria"
replace country = "Lebanon" if country=="10. Lebanon"
replace country = "Palestine, State of" if country=="15. Palestine"
replace country = "Saudi Arabia" if country=="17. Saudi Arabia"
replace country = "Sudan" if country=="19. Sudan"
replace country = "Tunisia" if country=="21. Tunisia"
replace country = "Yemen" if country=="22. Yemen"
replace country = "Egypt" if country=="5. Egypt"
replace country = "Iraq" if country=="7. Iraq"
replace country = "Jordan" if country=="8. Jordan"

merge m:1 country using "$data_dir/raw/countrycodes.dta"
drop if _merge==2
drop _merge

gen survey = "Arabarometer"
gen wave = 2

gen weight = wt

**********************************
*************TRUST VALUES*********
**********************************

gen trust_govt = 1 if q2011==1|q2011==2
replace trust_govt = 0 if q2011==3|q2011==4

gen trust_justice = 1 if q2012==1|q2012==2
replace trust_justice = 0 if q2012==3|q2012==4

gen trust_police = 1 if q2014==1|q2014==2
replace trust_police = 0 if q2014==3|q2014==4

gen trust_civil_service = .

gen trust_local_govt = .

gen trust_parties = 1 if q2015==1|q2015==2
replace trust_parties = 0 if q2015==3|q2015==4

gen trust_parliament = 1 if q2013==1|q2013==2
replace trust_parliament = 0 if q2013==3|q2013==4

gen trust_others = 1 if q103==1
replace trust_others = 0 if q103==2

gen trust_military = 1 if q2016==1|q2016==2
replace trust_military = 0 if q2016==3|q2016==4

pca trust_govt trust_justice trust_police trust_parties trust_military, comp(1)
predict mtrust_state
summ mtrust_state, de
gen trust_state = 1 if mtrust_state>=r(p50) & mtrust_state!=.
replace trust_state = 0 if mtrust_state<r(p50) & mtrust_state!=.

************************************************
*************INDIVIDUAL CHARACTERISTICS*********
************************************************

gen survey_year =.
replace survey_year = 2011 if ccode=="DZA"
replace survey_year = 2011 if ccode=="EGY"
replace survey_year = 2011 if ccode=="IRQ"
replace survey_year = 2010 if ccode=="JOR"
replace survey_year = 2010 if ccode=="LBN"
replace survey_year = 2010 if ccode=="PSE"
replace survey_year = 2011 if ccode=="SAU"
replace survey_year = 2010 if ccode=="SDN"
replace survey_year = 2011 if ccode=="TUN"
replace survey_year = 2011 if ccode=="YEM"

clonevar age = q1001
replace age = . if age==994|age==998|age==999|age==0

gen yob = survey_year-age

gen cohort = ""
forvalues i = 1870(10)2010 {
	replace cohort = "`i's" if yob>=`i' & yob<(`i'+10)
}

*sex
*			1   Male               
*			2   Female                                
*		  -99   Missing

gen sex = 1 if q1002==1
replace sex = 2 if q1002==2
replace sex = -99 if q1002==.

*religion
*			1   Do not belong to a denomination
*			2   Buddhist                
*			3   Jewish                  
*			4   Christian               
*			5   Muslim                  
*			6   Other 
*			7   Hindu 
*		  -99   Missing 

gen religion = .
replace religion = 5 if q1012==1
replace religion = 4 if q1012==2
replace religion = 3 if q1012==10001
replace religion = 6 if q1012==3|q1012==3001
replace religion = -99 if q1012==99999

*edu
*			1   Primary
*			2   Secondary
*			3   Tertiary
*			4   Other
*		  -99   Missing

gen edu = .
replace edu = 1 if q1003==2|q1003==3
replace edu = 2 if q1003==4
replace edu = 3 if q1003==5|q1003==6|q1003==7
replace edu = 4 if q1003==1
replace edu = -99 if q1003==0|q1003==.

*occupation
*			1   Managers
*			2   Professionals
*			3   Technicians and Associate Professionals
*			4   Clerical Support Workers
*			5   Service and Sales Workers
*			6   Skilled Agricultural, Forestry and Fishery Workers
*			7   Craft and Related Trades Workers
*			8   Plant and Machine Operators, and Assemblers
*			9   Elementary Occupations
*			0   Armed Forces Occupations
*	      -99   Missing

gen occupation = .
replace occupation = 1 if inlist(q1007, 1, 2, 8)
replace occupation = 2 if inlist(q1007, 3, 9)
replace occupation = 5 if inlist(q1007, 7)
replace occupation = 6 if inlist(q1007, 5)
replace occupation = 7 if inlist(q1007, 4, 10)
replace occupation = 0 if inlist(q1007, 6)
replace occupation = -99 if inlist(q1007, 0, 98, 99)
replace occupation = -99 if q1007==.

*employment_status
*			1   Employed
*			2   Unemployed
*			3   Other
*		  -99   Missing

gen employment_status = 1 if q1004==1
replace employment_status = 2 if q1004==2
replace employment_status = -99 if q1004==0|q1004==9|q1004==.

*income
*			1   Band 1
*			2   Band 2
*			3   Band 3
*			4   Band 4
*			5   Band 5
*			6   Band 6
*			7   Band 7
*			8   Band 8
*			9   Band 9
*		   10   Band 10
*		  -99   Missing

clonevar income_raw = v1015
replace income_raw = . if inlist(income_raw, 9999994, 9999996, 9999997, 9999998, 9999999)

egen panelid = group(country)
gen income = .
forvalues i = 1/10 {
	xtile income_bracket`i' = income_raw if panelid==`i' & income_raw!=., nq(10)
	replace income = income_bracket`i' if income==.
}
replace income = -99 if inlist(v1015, 9999994, 9999996, 9999997, 9999998, 9999999)
drop panelid income_bracket*

*ideology
*			1   left  
*			2         
*			3         
*			4         
*			5         
*			6         
*			7         
*			8         
*			9         
*			10  right
*		   -99  Missing

gen ideology = -99

*marital
*			1   Married
*			2   Not Married
*		  -99   Missing

gen marital = 1 if q1010==2 
replace marital = 2 if q1010==1|q1010==3|q1010==4
replace marital = -99 if q1010==0|q1010==993|q1010==996|q1010==999

*urban
*			1   Urban
*			2   Rural
*		  -99   Missing

gen urban = 1 if q13==1
replace urban = 2 if q13==2
replace urban = -99 if q13==.

*born_country
*			1   Born in country
*			0   Not born in country

gen born_country = .

keep S009 ccode country numcode country_short cow wave trust_state trust_govt trust_justice trust_police trust_civil_service trust_local_govt trust_parties trust_parliament trust_others trust_military  survey_year age yob cohort sex religion edu occupation employment_status income ideology marital urban survey born_country weight 

sort ccode wave survey_year

save "$data_dir/clean/arab2_clean.dta", replace

***************************************************************************************************************

*Arab 3

clear
use "$data_dir/raw/Arab Barometer/ABIII_English.dta"

rename country country_ab
decode country_ab, gen(country)

replace country = "Palestine, State of" if country=="Palestine"

merge m:1 country using "$data_dir/raw/countrycodes.dta"
drop if _merge==2
drop _merge

gen survey = "Arabarometer"
gen wave = 3

gen weight = wt

**********************************
*************TRUST VALUES*********
**********************************

gen trust_govt = 1 if q2011==1|q2011==2
replace trust_govt = 0 if q2011==3|q2011==4

gen trust_justice = .

gen trust_police = 1 if q2014==1|q2014==2
replace trust_police = 0 if q2014==3|q2014==4

gen trust_civil_service = .

gen trust_local_govt = .

gen trust_parties = .

gen trust_parliament = 1 if q2013==1|q2013==2
replace trust_parliament = 0 if q2013==3|q2013==4

gen trust_others = 1 if q103==1
replace trust_others = 0 if q103==2

gen trust_military = 1 if q2016==1|q2016==2
replace trust_military = 0 if q2016==3|q2016==4

pca trust_govt trust_police trust_parliament trust_military, comp(1)
predict mtrust_state
summ mtrust_state, de
gen trust_state = 1 if mtrust_state>=r(p50) & mtrust_state!=.
replace trust_state = 0 if mtrust_state<r(p50) & mtrust_state!=.

************************************************
*************INDIVIDUAL CHARACTERISTICS*********
************************************************

gen survey_year =.
replace survey_year = 2013 if ccode=="DZA"
replace survey_year = 2013 if ccode=="EGY"
replace survey_year = 2013 if ccode=="IRQ"
replace survey_year = 2012 if ccode=="JOR"
replace survey_year = 2014 if ccode=="KWT"
replace survey_year = 2013 if ccode=="LBN"
replace survey_year = 2014 if ccode=="LBY"
replace survey_year = 2013 if ccode=="MAR"
replace survey_year = 2012 if ccode=="PSE"
replace survey_year = 2013 if ccode=="SDN"
replace survey_year = 2013 if ccode=="TUN"
replace survey_year = 2013 if ccode=="YEM"

clonevar age = q1001
replace age = . if age==9999|age==0

gen yob = survey_year-age

gen cohort = ""
forvalues i = 1870(10)2010 {
	replace cohort = "`i's" if yob>=`i' & yob<(`i'+10)
}

*sex
*			1   Male               
*			2   Female                                
*		  -99   Missing

replace sex = -99 if q1002==.

*religion
*			1   Do not belong to a denomination
*			2   Buddhist                
*			3   Jewish                  
*			4   Christian               
*			5   Muslim                  
*			6   Other 
*			7   Hindu 
*		  -99   Missing 

gen religion = .
replace religion = 5 if q1012==1
replace religion = 4 if q1012==2
replace religion = 3 if q1012==4
replace religion = 6 if q1012==3
replace religion = -99 if q1012==0|q1012==98

*edu
*			1   Primary
*			2   Secondary
*			3   Tertiary
*			4   Other
*		  -99   Missing

gen edu = .
replace edu = 1 if q1003==2|q1003==3|q1003t==2|q1003t==3|q1003yem==2|q1003yem==3|q1003yem==4
replace edu = 2 if q1003==4|q1003t==4|q1003yem==5
replace edu = 3 if q1003==5|q1003==6|q1003==7|q1003t==5|q1003t==6|q1003yem==6|q1003yem==7|q1003yem==8
replace edu = 4 if q1003==1|q1003t==1|q1003yem==1
replace edu = -99 if q1003==0|q1003==99|q1003==.|q1003t==.|q1003yem==0|q1003yem==.

*occupation
*			1   Managers
*			2   Professionals
*			3   Technicians and Associate Professionals
*			4   Clerical Support Workers
*			5   Service and Sales Workers
*			6   Skilled Agricultural, Forestry and Fishery Workers
*			7   Craft and Related Trades Workers
*			8   Plant and Machine Operators, and Assemblers
*			9   Elementary Occupations
*			0   Armed Forces Occupations
*	      -99   Missing

gen occupation = .
replace occupation = 1 if inlist(q1007a, 1, 2, 8)
replace occupation = 2 if inlist(q1007a, 3, 9)
replace occupation = 5 if inlist(q1007a, 7)
replace occupation = 6 if inlist(q1007a, 5)
replace occupation = 7 if inlist(q1007a, 4, 10)
replace occupation = 0 if inlist(q1007a, 6)
replace occupation = -99 if inlist(q1007a, 0, 98, 99)
replace occupation = -99 if q1007a==.

*employment_status
*			1   Employed
*			2   Unemployed
*			3   Other
*		  -99   Missing

gen employment_status = 1 if q1004==1
replace employment_status = 2 if q1004==2
replace employment_status = -99 if q1004==0|q1004==9|q1004==.

*income
*			1   Band 1
*			2   Band 2
*			3   Band 3
*			4   Band 4
*			5   Band 5
*			6   Band 6
*			7   Band 7
*			8   Band 8
*			9   Band 9
*		   10   Band 10
*		  -99   Missing

clonevar income_raw = q1015
replace income_raw = . if inlist(income_raw, 9999994, 9999996, 9999997, 9999998, 9999999)

egen panelid = group(country)
gen income = .
forvalues i = 1/12 {
xtile income_bracket`i' = income_raw if panelid==`i' & income_raw!=., nq(10)
replace income = income_bracket`i' if income==.
}
replace income = -99 if inlist(q1015, 9999994, 9999996, 9999997, 9999998, 9999999)
drop panelid income_bracket*

*ideology
*			1   left  
*			2         
*			3         
*			4         
*			5         
*			6         
*			7         
*			8         
*			9         
*			10  right
*		   -99  Missing

gen ideology = -99

*marital
*			1   Married
*			2   Not Married
*		  -99   Missing

gen marital = 1 if q1010==2 
replace marital = 2 if q1010==1|q1010==3|q1010==4|q1010==5
replace marital = -99 if q1010==0|q1010==99998

*urban
*			1   Urban
*			2   Rural
*		  -99   Missing

gen urban = 1 if q13==1
replace urban = 2 if q13==2
replace urban = -99 if q13==.

*born_country
*			1   Born in country
*			0   Not born in country

gen born_country = .

keep S009 ccode country numcode country_short cow wave trust_state trust_govt trust_justice trust_police trust_civil_service trust_local_govt trust_parties trust_parliament trust_others trust_military  survey_year age yob cohort sex religion edu occupation employment_status income ideology marital urban survey born_country weight 

sort ccode wave survey_year

save "$data_dir/clean/arab3_clean.dta", replace

***************************************************************************************************************

*Arab 4

clear
use "$data_dir/raw/Arab Barometer/ABIV_English.dta"

rename country country_ab
decode country_ab, gen(country)

replace country = "Palestine, State of" if country=="Palestine"

merge m:1 country using "$data_dir/raw/countrycodes.dta"
drop if _merge==2
drop _merge

gen survey = "Arabarometer"
gen wave = 4

gen weight = wt

**********************************
*************TRUST VALUES*********
**********************************

gen trust_govt = 1 if q2011==1|q2011==2
replace trust_govt = 0 if q2011==3|q2011==4

gen trust_justice = 1 if q2012==1|q2012==2
replace trust_justice = 0 if q2012==3|q2012==4

gen trust_police = 1 if q2014==1|q2014==2
replace trust_police = 0 if q2014==3|q2014==4

gen trust_civil_service = .

gen trust_local_govt = .

gen trust_parties = 1 if q20120==1|q20120==2
replace trust_parties = 0 if q20120==3|q20120==4

gen trust_parliament = 1 if q2013==1|q2013==2
replace trust_parliament = 0 if q2013==3|q2013==4

gen trust_others = 1 if q103==1
replace trust_others = 0 if q103==2

gen trust_military = 1 if q2016==1|q2016==2
replace trust_military = 0 if q2016==3|q2016==4

pca trust_govt trust_justice trust_police trust_parties trust_parliament trust_military, comp(1)
predict mtrust_state
summ mtrust_state, de
gen trust_state = 1 if mtrust_state>=r(p50) & mtrust_state!=.
replace trust_state = 0 if mtrust_state<r(p50) & mtrust_state!=.

************************************************
*************INDIVIDUAL CHARACTERISTICS*********
************************************************

gen survey_year =.
replace survey_year = 2016 if ccode=="DZA"
replace survey_year = 2016 if ccode=="EGY"
replace survey_year = 2016 if ccode=="JOR"
replace survey_year = 2016 if ccode=="LBN"
replace survey_year = 2016 if ccode=="MAR"
replace survey_year = 2016 if ccode=="PSE"
replace survey_year = 2016 if ccode=="TUN"

clonevar age = q1001
replace age = . if age==999

gen yob = survey_year-age

gen cohort = ""
forvalues i = 1870(10)2010 {
	replace cohort = "`i's" if yob>=`i' & yob<(`i'+10)
}

*sex
*			1   Male               
*			2   Female                                
*		  -99   Missing

gen sex = 1 if q1002==1
replace sex = 2 if q1002==2
replace sex = -99 if q1002==.

*religion
*			1   Do not belong to a denomination
*			2   Buddhist                
*			3   Jewish                  
*			4   Christian               
*			5   Muslim                  
*			6   Other 
*			7   Hindu 
*		  -99   Missing 

gen religion = .
replace religion = 5 if q1012==1
replace religion = 4 if q1012==2
replace religion = 6 if q1012==3
replace religion = -99 if q1012==.

*edu
*			1   Primary
*			2   Secondary
*			3   Tertiary
*			4   Other
*		  -99   Missing

gen edu = .
replace edu = 1 if q1003==2|q1003==3|t1003==2|t1003==3
replace edu = 2 if q1003==4|t1003==4
replace edu = 3 if q1003==5|q1003==6|q1003==7|t1003==5|t1003==6
replace edu = 4 if q1003==1|t1003==1
replace edu = -99 if q1003==99|q1003==.

*occupation
*			1   Managers
*			2   Professionals
*			3   Technicians and Associate Professionals
*			4   Clerical Support Workers
*			5   Service and Sales Workers
*			6   Skilled Agricultural, Forestry and Fishery Workers
*			7   Craft and Related Trades Workers
*			8   Plant and Machine Operators, and Assemblers
*			9   Elementary Occupations
*			0   Armed Forces Occupations
*	      -99   Missing

gen occupation = .
replace occupation = 1 if inlist(q1007a, 1, 2)
replace occupation = 2 if inlist(q1007a, 3, 9)
replace occupation = 5 if inlist(q1007a, 7)
replace occupation = 6 if inlist(q1007a, 5)
replace occupation = 7 if inlist(q1007a, 4, 11)
replace occupation = 0 if inlist(q1007a, 6)
replace occupation = -99 if inlist(q1007a, 94, 98, 99)
replace occupation = -99 if q1007a==.

*employment_status
*			1   Employed
*			2   Unemployed
*			3   Other
*		  -99   Missing

gen employment_status = 1 if q1004==1
replace employment_status = 2 if q1004==2
replace employment_status = -99 if q1004==0|q1004==9|q1004==.

*income
*			1   Band 1
*			2   Band 2
*			3   Band 3
*			4   Band 4
*			5   Band 5
*			6   Band 6
*			7   Band 7
*			8   Band 8
*			9   Band 9
*		   10   Band 10
*		  -99   Missing

gen income = .
replace income = 1 if q1015balg==1 & income==.
replace income = 2 if q1015balg==2 & income==.
replace income = 3 if q1015balg==3 & income==.
replace income = 4 if q1015balg==4 & income==.
replace income = 5 if (q1015balg==5|q1015balg==6) & income==.
replace income = 6 if q1015calg==1 & income==.
replace income = 7 if q1015calg==2 & income==.
replace income = 8 if q1015calg==3 & income==.
replace income = 9 if q1015calg==4 & income==.
replace income = 10 if (q1015calg==5|q1015calg==6) & income==.

replace income = 1 if q1015begy==1 & income==.
replace income = 2 if q1015begy==2 & income==.
replace income = 3 if q1015begy==3 & income==.
replace income = 4 if q1015begy==4 & income==.
replace income = 5 if (q1015begy==5|q1015begy==6) & income==.
replace income = 6 if q1015cegy==1 & income==.
replace income = 7 if q1015cegy==2 & income==.
replace income = 8 if q1015cegy==3 & income==.
replace income = 9 if q1015cegy==4 & income==.
replace income = 10 if (q1015cegy==5|q1015cegy==6) & income==.

replace income = 1 if q1015bjor==1 & income==.
replace income = 2 if q1015bjor==2 & income==.
replace income = 3 if q1015bjor==3 & income==.
replace income = 4 if q1015bjor==4 & income==.
replace income = 5 if (q1015bjor==5|q1015bjor==6) & income==.
replace income = 6 if q1015cjor==1 & income==.
replace income = 7 if q1015cjor==2 & income==.
replace income = 8 if q1015cjor==3 & income==.
replace income = 9 if q1015cjor==4 & income==.
replace income = 10 if (q1015cjor==5|q1015cjor==6) & income==.

replace income = 1 if q1015bleb==1 & income==.
replace income = 2 if q1015bleb==2 & income==.
replace income = 3 if q1015bleb==3 & income==.
replace income = 4 if q1015bleb==4 & income==.
replace income = 5 if (q1015bleb==5|q1015bleb==6) & income==.
replace income = 6 if q1015cleb==1 & income==.
replace income = 7 if q1015cleb==2 & income==.
replace income = 8 if q1015cleb==3 & income==.
replace income = 9 if q1015cleb==4 & income==.
replace income = 10 if (q1015cleb==5|q1015cleb==6) & income==.

replace income = 1 if q1015bmor==1 & income==.
replace income = 2 if q1015bmor==2 & income==.
replace income = 3 if q1015bmor==3 & income==.
replace income = 4 if q1015bmor==4 & income==.
replace income = 5 if (q1015bmor==5|q1015bmor==6) & income==.
replace income = 6 if q1015cmor==1 & income==.
replace income = 7 if q1015cmor==2 & income==.
replace income = 8 if q1015cmor==3 & income==.
replace income = 9 if q1015cmor==4 & income==.
replace income = 10 if (q1015cmor==5|q1015cmor==6) & income==.

replace income = 1 if q1015bpal==1 & income==.
replace income = 2 if q1015bpal==2 & income==.
replace income = 3 if q1015bpal==3 & income==.
replace income = 4 if q1015bpal==4 & income==.
replace income = 5 if (q1015bpal==5|q1015bpal==6) & income==.
replace income = 6 if q1015cpal==1 & income==.
replace income = 7 if q1015cpal==2 & income==.
replace income = 8 if q1015cpal==3 & income==.
replace income = 9 if q1015cpal==4 & income==.
replace income = 10 if (q1015cpal==5|q1015cpal==6) & income==.

replace income = 1 if q1015btun==1 & income==.
replace income = 2 if q1015btun==2 & income==.
replace income = 3 if q1015btun==3 & income==.
replace income = 4 if q1015btun==4 & income==.
replace income = 5 if (q1015btun==5|q1015btun==6) & income==.
replace income = 6 if q1015ctun==1 & income==.
replace income = 7 if q1015ctun==2 & income==.
replace income = 8 if q1015ctun==3 & income==.
replace income = 9 if q1015ctun==4 & income==.
replace income = 10 if (q1015ctun==5|q1015ctun==6) & income==.

local varlist q1015balg q1015begy q1015bjor q1015bleb q1015bmor q1015bpal q1015btun q1015calg q1015cegy q1015cjor q1015cleb q1015cmor q1015cpal q1015ctun
foreach var of local varlist {
	replace income = -99 if (`var'==98|`var'==99)
}

*ideology
*			1   left  
*			2         
*			3         
*			4         
*			5         
*			6         
*			7         
*			8         
*			9         
*			10  right
*		   -99  Missing

gen ideology = -99

*marital
*			1   Married
*			2   Not Married
*		  -99   Missing

gen marital = 1 if q1010==2|q1010==6
replace marital = 2 if q1010==1|q1010==3|q1010==4|q1010==5
replace marital = -99 if q1010==.|q1010==99

*urban
*			1   Urban
*			2   Rural
*		  -99   Missing

gen urban = 1 if q13==1
replace urban = 2 if q13==2
replace urban = -99 if q13==.|q13==3

*born_country
*			1   Born in country
*			0   Not born in country

gen born_country = .

keep S009 ccode country numcode country_short cow wave trust_state trust_govt trust_justice trust_police trust_civil_service trust_local_govt trust_parties trust_parliament trust_others trust_military  survey_year age yob cohort sex religion edu occupation employment_status income ideology marital urban survey born_country weight 

sort ccode wave survey_year

save "$data_dir/clean/arab4_clean.dta", replace

***************************************************************************************************************

*Arab 5

clear
use "$data_dir/raw/Arab Barometer/ABV_Release_Data.dta"

rename country country_ab
decode country_ab, gen(country)

replace country = "Palestine, State of" if country=="Palestine"

merge m:1 country using "$data_dir/raw/countrycodes.dta"
drop if _merge==2
drop _merge

gen survey = "Arabarometer"
//gen wave = 5 already a variable

gen weight = wt

**********************************
*************TRUST VALUES*********
**********************************

gen trust_govt = 1 if Q201A_1==1|Q201A_1==2
replace trust_govt = 0 if Q201A_1==3|Q201A_1==4

gen trust_justice = 1 if Q201A_2==1|Q201A_2==2
replace trust_justice = 0 if Q201A_2==3|Q201A_2==4

gen trust_police = 1 if Q201A_42==1|Q201A_42==2
replace trust_police = 0 if Q201A_42==3|Q201A_42==4

gen trust_civil_service = 1 if Q201C_32==1|Q201C_32==2
replace trust_civil_service = 0 if Q201C_32==3|Q201C_32==4

gen trust_local_govt = 1 if Q201A_5==1|Q201A_5==2
replace trust_local_govt = 0 if Q201A_5==3|Q201A_5==4

gen trust_parties = 1 if Q201B_20==1|Q201B_20==2
replace trust_parties = 0 if Q201B_20==3|Q201B_20==4

gen trust_parliament = 1 if Q201A_3==1|Q201A_3==2
replace trust_parliament = 0 if Q201A_3==3|Q201A_3==4

gen trust_others = 1 if Q103==1
replace trust_others = 0 if Q103==2

gen trust_military = 1 if Q201B_6==1|Q201B_6==2
replace trust_military = 0 if Q201B_6==3|Q201B_6==4

pca trust_govt trust_justice trust_police trust_local_govt trust_parliament, comp(1)
predict mtrust_state
summ mtrust_state, de
gen trust_state = 1 if mtrust_state>=r(p50) & mtrust_state!=.
replace trust_state = 0 if mtrust_state<r(p50) & mtrust_state!=.

************************************************
*************INDIVIDUAL CHARACTERISTICS*********
************************************************

gen survey_year =.
replace survey_year = 2019 if ccode=="DZA"
replace survey_year = 2018 if ccode=="EGY"
replace survey_year = 2018 if ccode=="IRQ"
replace survey_year = 2018 if ccode=="JOR"
replace survey_year = 2019 if ccode=="KWT"
replace survey_year = 2018 if ccode=="LBN"
replace survey_year = 2019 if ccode=="LBY"
replace survey_year = 2018 if ccode=="MAR"
replace survey_year = 2018 if ccode=="PSE"
replace survey_year = 2018 if ccode=="SDN"
replace survey_year = 2018 if ccode=="TUN"
replace survey_year = 2018 if ccode=="YEM"

clonevar age = Q1001
replace age = . if age==99999

gen yob = survey_year-age

gen cohort = ""
forvalues i = 1870(10)2010 {
	replace cohort = "`i's" if yob>=`i' & yob<(`i'+10)
}

*sex
*			1   Male               
*			2   Female                                
*		  -99   Missing

gen sex = 1 if Q1002==1
replace sex = 2 if Q1002==2
replace sex = -99 if Q1002==.

*religion
*			1   Do not belong to a denomination
*			2   Buddhist                
*			3   Jewish                  
*			4   Christian               
*			5   Muslim                  
*			6   Other 
*			7   Hindu 
*		  -99   Missing 

gen religion = .
replace religion = 1 if Q1012==4
replace religion = 5 if Q1012==1
replace religion = 4 if Q1012==2
replace religion = 3 if Q1012==3
replace religion = 6 if Q1012==5
replace religion = -99 if Q1012==99|Q1012==.

*edu
*			1   Primary
*			2   Secondary
*			3   Tertiary
*			4   Other
*		  -99   Missing

gen edu = .
replace edu = 1 if Q1003==2|Q1003==3
replace edu = 2 if Q1003==4
replace edu = 3 if Q1003==5|Q1003==6|Q1003==7
replace edu = 4 if Q1003==1
replace edu = -99 if Q1003==98|Q1003==99|Q1003==.

*occupation
*			1   Managers
*			2   Professionals
*			3   Technicians and Associate Professionals
*			4   Clerical Support Workers
*			5   Service and Sales Workers
*			6   Skilled Agricultural, Forestry and Fishery Workers
*			7   Craft and Related Trades Workers
*			8   Plant and Machine Operators, and Assemblers
*			9   Elementary Occupations
*			0   Armed Forces Occupations
*	      -99   Missing

gen occupation = -99

*employment_status
*			1   Employed
*			2   Unemployed
*			3   Other
*		  -99   Missing

gen employment_status = .
replace employment_status = 1 if Q1005==1|Q1005==2
replace employment_status = 2 if Q1005==6
replace employment_status = 3 if Q1005==3|Q1005==4|Q1005==5|Q1005==7
replace employment_status = -99 if Q1005==98|Q1005==99

*income
*			1   Band 1
*			2   Band 2
*			3   Band 3
*			4   Band 4
*			5   Band 5
*			6   Band 6
*			7   Band 7
*			8   Band 8
*			9   Band 9
*		   10   Band 10
*		  -99   Missing

gen income = .

local varlist Q1015B_LEB Q1015C_LEB Q1015B_JO Q1015C_JO Q1015B_PAL Q1015C_PAL Q1015B_MO Q1015C_MO Q1015B_TUN Q1015C_TUN Q1015B_EG Q1015C_EG Q1015B_SUD Q1015C_SUD Q1015_YEM Q1015B_IR Q1015C_IR Q1015B_AL Q1015C_AL Q1015B_LI Q1015C_LI Q1015B_KU Q1015C_KU
foreach var of local varlist {
	replace income = -99 if `var'==98|`var'==99|`var'==.
}

replace income = 1 if Q1015B_LEB==1 & income==.
replace income = 2 if Q1015B_LEB==2 & income==.
replace income = 3 if Q1015B_LEB==3 & income==.
replace income = 4 if Q1015C_LEB==1 & income==.
replace income = 5 if Q1015C_LEB==2 & income==.
replace income = 6 if Q1015C_LEB==3 & income==.
replace income = 7 if Q1015C_LEB==4 & income==.
replace income = 8 if Q1015C_LEB==5 & income==.
replace income = 9 if Q1015C_LEB==6 & income==.

replace income = 1 if Q1015B_JO==1 & income==.
replace income = 2 if Q1015B_JO==2 & income==.
replace income = 3 if Q1015B_JO==3 & income==.
replace income = 4 if Q1015B_JO==4 & income==.
replace income = 5 if (Q1015B_JO==5|Q1015B_JO==6) & income==.
replace income = 6 if Q1015C_JO==1 & income==.
replace income = 7 if Q1015C_JO==2 & income==.
replace income = 8 if Q1015C_JO==3 & income==.
replace income = 9 if Q1015C_JO==4 & income==.
replace income = 10 if (Q1015C_JO==5|Q1015C_JO==6) & income==.

replace income = 1 if Q1015B_PAL==1 & income==.
replace income = 2 if Q1015B_PAL==2 & income==.
replace income = 3 if Q1015B_PAL==3 & income==.
replace income = 4 if Q1015B_PAL==4 & income==.
replace income = 5 if (Q1015B_PAL==5|Q1015B_PAL==6) & income==.
replace income = 6 if Q1015C_PAL==1 & income==.
replace income = 7 if Q1015C_PAL==2 & income==.
replace income = 8 if Q1015C_PAL==3 & income==.
replace income = 9 if Q1015C_PAL==4 & income==.
replace income = 10 if (Q1015C_PAL==5|Q1015C_PAL==6) & income==.

replace income = 1 if Q1015B_MO==1 & income==.
replace income = 2 if Q1015B_MO==2 & income==.
replace income = 3 if Q1015B_MO==3 & income==.
replace income = 4 if Q1015B_MO==4 & income==.
replace income = 5 if (Q1015B_MO==5|Q1015B_MO==6) & income==.
replace income = 6 if Q1015C_MO==1 & income==.
replace income = 7 if Q1015C_MO==2 & income==.
replace income = 8 if Q1015C_MO==3 & income==.
replace income = 9 if Q1015C_MO==4 & income==.
replace income = 10 if (Q1015C_MO==5|Q1015C_MO==6) & income==.

replace income = 1 if Q1015B_SUD==1 & income==.
replace income = 2 if Q1015B_SUD==2 & income==.
replace income = 3 if Q1015B_SUD==3 & income==.
replace income = 4 if Q1015B_SUD==4 & income==.
replace income = 5 if (Q1015B_SUD==5|Q1015B_SUD==6) & income==.
replace income = 6 if Q1015C_SUD==1 & income==.
replace income = 7 if Q1015C_SUD==2 & income==.
replace income = 8 if Q1015C_SUD==3 & income==.
replace income = 9 if Q1015C_SUD==4 & income==.
replace income = 10 if (Q1015C_SUD==5|Q1015C_SUD==6) & income==.

xtile yem_income = Q1015_YEM, nq(10)

replace income = yem_income if income==.

replace income = 1 if Q1015B_IR==1 & income==.
replace income = 2 if Q1015B_IR==2 & income==.
replace income = 3 if Q1015B_IR==3 & income==.
replace income = 4 if Q1015B_IR==4 & income==.
replace income = 5 if (Q1015B_IR==5|Q1015B_IR==6) & income==.
replace income = 6 if Q1015C_IR==1 & income==.
replace income = 7 if Q1015C_IR==2 & income==.
replace income = 8 if Q1015C_IR==3 & income==.
replace income = 9 if Q1015C_IR==4 & income==.
replace income = 10 if (Q1015C_IR==5|Q1015C_IR==6) & income==.

replace income = 1 if Q1015B_KU==1 & income==.
replace income = 2 if Q1015B_KU==2 & income==.
replace income = 3 if Q1015B_KU==3 & income==.
replace income = 4 if Q1015B_KU==4 & income==.
replace income = 5 if (Q1015B_KU==5|Q1015B_KU==6) & income==.
replace income = 6 if Q1015C_KU==1 & income==.
replace income = 7 if Q1015C_KU==2 & income==.
replace income = 8 if Q1015C_KU==3 & income==.
replace income = 9 if Q1015C_KU==4 & income==.
replace income = 10 if (Q1015C_KU==5|Q1015C_KU==6) & income==.

replace income = 1 if Q1015B_TUN==1 & income==.
replace income = 2 if Q1015B_TUN==2 & income==.
replace income = 3 if Q1015B_TUN==3 & income==.
replace income = 4 if Q1015B_TUN==4 & income==.
replace income = 5 if (Q1015B_TUN==5|Q1015B_TUN==6) & income==.
replace income = 6 if Q1015C_TUN==1 & income==.
replace income = 7 if Q1015C_TUN==2 & income==.
replace income = 8 if Q1015C_TUN==3 & income==.
replace income = 9 if Q1015C_TUN==4 & income==.
replace income = 10 if (Q1015C_TUN==5|Q1015C_TUN==6) & income==.

*ideology
*			1   left  
*			2         
*			3         
*			4         
*			5         
*			6         
*			7         
*			8         
*			9         
*			10  right
*		   -99  Missing

gen ideology = -99

*marital
*			1   Married
*			2   Not Married
*		  -99   Missing

gen marital = 1 if Q1010==4|Q1010==2
replace marital = 2 if Q1010==1|Q1010==3|Q1010==5|Q1010==6|Q1010==7
replace marital = -99 if Q1010==.|Q1010==99

*urban
*			1   Urban
*			2   Rural
*		  -99   Missing

gen urban = 1 if q13==1
replace urban = 2 if q13==2
replace urban = -99 if q13==.|q13==3

*born_country
*			1   Born in country
*			0   Not born in country

gen born_country = .

keep S009 ccode country numcode country_short cow wave trust_state trust_govt trust_justice trust_police trust_civil_service trust_local_govt trust_parties trust_parliament trust_others trust_military  survey_year age yob cohort sex religion edu occupation employment_status income ideology marital urban survey born_country weight 

sort ccode wave survey_year

save "$data_dir/clean/arab5_clean.dta", replace

***************************************************************************************************************

*Arab 6

clear
use "$data_dir/raw/Arab Barometer/Arab_Barometer_Wave_6_Part_3_ENG_RELEASE.dta"

rename COUNTRY country_ab
decode country_ab, gen(country)

merge m:1 country using "$data_dir/raw/countrycodes.dta"
drop if _merge==2
drop _merge

gen survey = "Arabarometer"
gen wave = 6

gen weight = WT

**********************************
*************TRUST VALUES*********
**********************************

gen trust_govt = 1 if Q201A_1==1|Q201A_1==2
replace trust_govt = 0 if Q201A_1==3|Q201A_1==4

gen trust_justice = 1 if Q201A_2==1|Q201A_2==2
replace trust_justice = 0 if Q201A_2==3|Q201A_2==4

gen trust_police = .

gen trust_civil_service = .

gen trust_local_govt = .

gen trust_parties = .

gen trust_parliament = .

gen trust_others = .

gen trust_military = 1 if Q201B_6==1|Q201B_6==2
replace trust_military = 0 if Q201B_6==3|Q201B_6==4

pca trust_govt trust_justice trust_military, comp(1)
predict mtrust_state
summ mtrust_state, de
gen trust_state = 1 if mtrust_state>=r(p50) & mtrust_state!=.
replace trust_state = 0 if mtrust_state<r(p50) & mtrust_state!=.

************************************************
*************INDIVIDUAL CHARACTERISTICS*********
************************************************

gen survey_year =.
replace survey_year = 2021

clonevar age = Q1001
replace age = . if age==99999|age==100

gen yob = survey_year-age

gen cohort = ""
forvalues i = 1870(10)2010 {
	replace cohort = "`i's" if yob>=`i' & yob<(`i'+10)
}

*sex
*			1   Male               
*			2   Female                                
*		  -99   Missing

gen sex = 1 if Q1002==1
replace sex = 2 if Q1002==2
replace sex = -99 if Q1002==.

*religion
*			1   Do not belong to a denomination
*			2   Buddhist                
*			3   Jewish                  
*			4   Christian               
*			5   Muslim                  
*			6   Other 
*			7   Hindu 
*		  -99   Missing 

gen religion = .
replace religion = 1 if Q1012==4
replace religion = 5 if Q1012==1
replace religion = 4 if Q1012==2
replace religion = 6 if Q1012==3
replace religion = -99 if Q1012==99|Q1012==.

*edu
*			1   Primary
*			2   Secondary
*			3   Tertiary
*			4   Other
*		  -99   Missing

gen edu = .
replace edu = 1 if Q1003==2|Q1003==3
replace edu = 2 if Q1003==4
replace edu = 3 if Q1003==5|Q1003==6|Q1003==7
replace edu = 4 if Q1003==1
replace edu = -99 if Q1003==99|Q1003==.

*occupation
*			1   Managers
*			2   Professionals
*			3   Technicians and Associate Professionals
*			4   Clerical Support Workers
*			5   Service and Sales Workers
*			6   Skilled Agricultural, Forestry and Fishery Workers
*			7   Craft and Related Trades Workers
*			8   Plant and Machine Operators, and Assemblers
*			9   Elementary Occupations
*			0   Armed Forces Occupations
*	      -99   Missing

gen occupation = -99

*employment_status
*			1   Employed
*			2   Unemployed
*			3   Other
*		  -99   Missing

gen employment_status = .
replace employment_status = 1 if Q1005==1|Q1005==2
replace employment_status = 2 if Q1005==6
replace employment_status = 3 if Q1005==3|Q1005==4|Q1005==5|Q1005==7
replace employment_status = -99 if Q1005==98|Q1005==99

*income
*			1   Band 1
*			2   Band 2
*			3   Band 3
*			4   Band 4
*			5   Band 5
*			6   Band 6
*			7   Band 7
*			8   Band 8
*			9   Band 9
*		   10   Band 10
*		  -99   Missing

gen income = -99

*ideology
*			1   left  
*			2         
*			3         
*			4         
*			5         
*			6         
*			7         
*			8         
*			9         
*			10  right
*		   -99  Missing

gen ideology = -99

*marital
*			1   Married
*			2   Not Married
*		  -99   Missing

gen marital = 1 if Q1010==4|Q1010==2
replace marital = 2 if Q1010==1|Q1010==3|Q1010==5|Q1010==6|Q1010==7
replace marital = -99 if Q1010==.|Q1010==99

*urban
*			1   Urban
*			2   Rural
*		  -99   Missing

gen urban = 1 if Q13A==1
replace urban = 2 if Q13A==2|Q13A==3
replace urban = -99 if Q13A==.|Q13A==4|Q13A==5|Q13A==97|Q13A==98|Q13A==99

*born_country
*			1   Born in country
*			0   Not born in country

gen born_country = .

keep S009 ccode country numcode country_short cow wave trust_state trust_govt trust_justice trust_police trust_civil_service trust_local_govt trust_parties trust_parliament trust_others trust_military  survey_year age yob cohort sex religion edu occupation employment_status income ideology marital urban survey born_country weight 

sort ccode wave survey_year

save "$data_dir/clean/arab6_clean.dta", replace

***************************************************************************************************************

*Arab 7

clear
use "$data_dir/raw/Arab Barometer/AB7_ENG_Release_Version6.dta"

rename COUNTRY country_ab
decode country_ab, gen(country)

replace country = "Palestine, State of" if country=="Palestine"

merge m:1 country using "$data_dir/raw/countrycodes.dta"
drop if _merge==2
drop _merge

gen survey = "Arabarometer"
gen wave = 7

gen weight = WT

**********************************
*************TRUST VALUES*********
**********************************

gen trust_govt = 1 if Q201A_1==1|Q201A_1==2
replace trust_govt = 0 if Q201A_1==3|Q201A_1==4

gen trust_justice = 1 if Q201A_2==1|Q201A_2==2
replace trust_justice = 0 if Q201A_2==3|Q201A_2==4

gen trust_police = 1 if Q201A_42A_MOR==1|Q201A_42A_MOR==2 //for some reason only applicable for Morocco
replace trust_police = 0 if Q201A_42A_MOR==3|Q201A_42A_MOR==4

gen trust_civil_service = .

gen trust_local_govt = 1 if Q201A_5==1|Q201A_5==2
replace trust_local_govt = 0 if Q201A_5==3|Q201A_5==4

gen trust_parties = .

gen trust_parliament = 1 if Q201A_3==1|Q201A_3==2
replace trust_parliament = 0 if Q201A_3==3|Q201A_3==4

gen trust_others = 1 if Q103==1
replace trust_others = 0 if Q103==2

gen trust_military = 1 if Q201B_6==1|Q201B_6==2
replace trust_military = 0 if Q201B_6==3|Q201B_6==4

pca trust_govt trust_justice trust_local_govt trust_parliament trust_military, comp(1)
predict mtrust_state
summ mtrust_state, de
gen trust_state = 1 if mtrust_state>=r(p50) & mtrust_state!=.
replace trust_state = 0 if mtrust_state<r(p50) & mtrust_state!=.

************************************************
*************INDIVIDUAL CHARACTERISTICS*********
************************************************

gen survey_year =.
replace survey_year = 2022 if ccode=="DZA"
replace survey_year = 2022 if ccode=="EGY"
replace survey_year = 2021 if ccode=="IRQ"
replace survey_year = 2022 if ccode=="JOR"
replace survey_year = 2022 if ccode=="KWT"
replace survey_year = 2021 if ccode=="LBN"
replace survey_year = 2022 if ccode=="LBY"
replace survey_year = 2021 if ccode=="MRT"
replace survey_year = 2022 if ccode=="MAR"
replace survey_year = 2021 if ccode=="PSE"
replace survey_year = 2022 if ccode=="SDN"
replace survey_year = 2021 if ccode=="TUN"

clonevar age = Q1001
replace age = . if age==99999

gen yob = survey_year-age

gen cohort = ""
forvalues i = 1870(10)2010 {
	replace cohort = "`i's" if yob>=`i' & yob<(`i'+10)
}

*sex
*			1   Male               
*			2   Female                                
*		  -99   Missing

gen sex = 1 if Q1002==1
replace sex = 2 if Q1002==2
replace sex = -99 if Q1002==.

*religion
*			1   Do not belong to a denomination
*			2   Buddhist                
*			3   Jewish                  
*			4   Christian               
*			5   Muslim                  
*			6   Other 
*			7   Hindu 
*		  -99   Missing 

gen religion = .
replace religion = 1 if Q1012==4
replace religion = 5 if Q1012==1
replace religion = 4 if Q1012==2
replace religion = 6 if Q1012==3
replace religion = -99 if Q1012==99|Q1012==.

*edu
*			1   Primary
*			2   Secondary
*			3   Tertiary
*			4   Other
*		  -99   Missing

gen edu = .
replace edu = 1 if Q1003==2|Q1003==3
replace edu = 2 if Q1003==4
replace edu = 3 if Q1003==5|Q1003==6|Q1003==7
replace edu = 4 if Q1003==1
replace edu = -99 if Q1003==98|Q1003==99|Q1003==.

*occupation
*			1   Managers
*			2   Professionals
*			3   Technicians and Associate Professionals
*			4   Clerical Support Workers
*			5   Service and Sales Workers
*			6   Skilled Agricultural, Forestry and Fishery Workers
*			7   Craft and Related Trades Workers
*			8   Plant and Machine Operators, and Assemblers
*			9   Elementary Occupations
*			0   Armed Forces Occupations
*	      -99   Missing

gen occupation = -99

*employment_status
*			1   Employed
*			2   Unemployed
*			3   Other
*		  -99   Missing

gen employment_status = .
replace employment_status = 1 if Q1005==1|Q1005==2
replace employment_status = 2 if Q1005==6
replace employment_status = 3 if Q1005==3|Q1005==4|Q1005==5|Q1005==7
replace employment_status = -99 if Q1005==98|Q1005==99

*income
*			1   Band 1
*			2   Band 2
*			3   Band 3
*			4   Band 4
*			5   Band 5
*			6   Band 6
*			7   Band 7
*			8   Band 8
*			9   Band 9
*		   10   Band 10
*		  -99   Missing

clonevar income_raw = Q1015
replace income_raw = . if income_raw==999999998|income_raw==999999999

egen panelid = group(country)
gen income = .
forvalues i = 1/12 {
	xtile income_bracket`i' = income_raw if panelid==`i' & income_raw!=., nq(10)
	replace income = income_bracket`i' if income==.
}
replace income = -99 if inlist(Q1015, 999999998, 999999999)
replace income = -99 if income==.
drop panelid income_bracket*

*ideology
*			1   left  
*			2         
*			3         
*			4         
*			5         
*			6         
*			7         
*			8         
*			9         
*			10  right
*		   -99  Missing

gen ideology = -99

*marital
*			1   Married
*			2   Not Married
*		  -99   Missing

gen marital = 1 if Q1010==4|Q1010==2
replace marital = 2 if Q1010==1|Q1010==3|Q1010==5|Q1010==6|Q1010==7
replace marital = -99 if Q1010==.|Q1010==99

*urban
*			1   Urban
*			2   Rural
*		  -99   Missing

gen urban = 1 if Q13==1
replace urban = 2 if Q13==2
replace urban = -99 if Q13==.|Q13==3

*born_country
*			1   Born in country
*			0   Not born in country

gen born_country = .

keep S009 ccode country numcode country_short cow wave trust_state trust_govt trust_justice trust_police trust_civil_service trust_local_govt trust_parties trust_parliament trust_others trust_military  survey_year age yob cohort sex religion edu occupation employment_status income ideology marital urban survey born_country weight 

sort ccode wave survey_year

save "$data_dir/clean/arab7_clean.dta", replace

***************************************************************************************************************

*Arab 8

clear
use "$data_dir/raw/Arab Barometer/ArabBarometer_WaveVIII_English_v3.dta"

rename COUNTRY country_ab
decode country_ab, gen(country)

replace country = "Palestine, State of" if country=="Palestine"

merge m:1 country using "$data_dir/raw/countrycodes.dta"
drop if _merge==2
drop _merge

gen survey = "Arabarometer"
gen wave = 7

gen weight = WT

**********************************
*************TRUST VALUES*********
**********************************

gen trust_govt = 1 if Q201A_1==1|Q201A_1==2
replace trust_govt = 0 if Q201A_1==3|Q201A_1==4

gen trust_justice = 1 if Q201A_2==1|Q201A_2==2
replace trust_justice = 0 if Q201A_2==3|Q201A_2==4

gen trust_police = 1 if Q201B_4==1|Q201B_4==2 //for some reason only applicable for Morocco
replace trust_police = 0 if Q201B_4==3|Q201B_4==4

gen trust_civil_service = .

gen trust_local_govt = 1 if Q201A_5==1|Q201A_5==2
replace trust_local_govt = 0 if Q201A_5==3|Q201A_5==4

gen trust_parties = .

gen trust_parliament = 1 if Q201A_3==1|Q201A_3==2
replace trust_parliament = 0 if Q201A_3==3|Q201A_3==4

gen trust_others = 1 if Q103==1
replace trust_others = 0 if Q103==2

gen trust_military = 1 if Q201B_6==1|Q201B_6==2
replace trust_military = 0 if Q201B_6==3|Q201B_6==4

pca trust_govt trust_justice trust_local_govt trust_parliament trust_military, comp(1)
predict mtrust_state
summ mtrust_state, de
gen trust_state = 1 if mtrust_state>=r(p50) & mtrust_state!=.
replace trust_state = 0 if mtrust_state<r(p50) & mtrust_state!=.

************************************************
*************INDIVIDUAL CHARACTERISTICS*********
************************************************

gen survey_year = substr(DATE, 1, 4)
destring survey_year, replace

clonevar age = Q1001
replace age = . if age==99999

gen yob = survey_year-age

gen cohort = ""
forvalues i = 1870(10)2010 {
	replace cohort = "`i's" if yob>=`i' & yob<(`i'+10)
}

*sex
*			1   Male               
*			2   Female                                
*		  -99   Missing

gen sex = 1 if Q1002==1
replace sex = 2 if Q1002==2
replace sex = -99 if Q1002==.

*religion
*			1   Do not belong to a denomination
*			2   Buddhist                
*			3   Jewish                  
*			4   Christian               
*			5   Muslim                  
*			6   Other 
*			7   Hindu 
*		  -99   Missing 

gen religion = .
replace religion = 1 if Q1012==4
replace religion = 5 if Q1012==1
replace religion = 4 if Q1012==2
replace religion = -99 if Q1012==90|Q1012==99|Q1012==.

*edu
*			1   Primary
*			2   Secondary
*			3   Tertiary
*			4   Other
*		  -99   Missing

gen edu = .
replace edu = 1 if Q1003==2|Q1003==3
replace edu = 2 if Q1003==4
replace edu = 3 if Q1003==5|Q1003==6|Q1003==7
replace edu = 4 if Q1003==1
replace edu = -99 if Q1003==98|Q1003==99|Q1003==.

*occupation
*			1   Managers
*			2   Professionals
*			3   Technicians and Associate Professionals
*			4   Clerical Support Workers
*			5   Service and Sales Workers
*			6   Skilled Agricultural, Forestry and Fishery Workers
*			7   Craft and Related Trades Workers
*			8   Plant and Machine Operators, and Assemblers
*			9   Elementary Occupations
*			0   Armed Forces Occupations
*	      -99   Missing

gen occupation = -99

*employment_status
*			1   Employed
*			2   Unemployed
*			3   Other
*		  -99   Missing

gen employment_status = .
replace employment_status = 1 if Q1005==1|Q1005==2
replace employment_status = 2 if Q1005==6
replace employment_status = 3 if Q1005==3|Q1005==4|Q1005==5
replace employment_status = -99 if Q1005==98|Q1005==99

*income
*			1   Band 1
*			2   Band 2
*			3   Band 3
*			4   Band 4
*			5   Band 5
*			6   Band 6
*			7   Band 7
*			8   Band 8
*			9   Band 9
*		   10   Band 10
*		  -99   Missing

clonevar income_raw = Q1015
replace income_raw = . if income_raw==999999998|income_raw==999999999

egen panelid = group(country)
gen income = .
forvalues i = 1/8 {
	xtile income_bracket`i' = income_raw if panelid==`i' & income_raw!=., nq(10)
	replace income = income_bracket`i' if income==.
}
replace income = -99 if inlist(Q1015, 999999998, 999999999)
replace income = -99 if income==.
drop panelid income_bracket*

*ideology
*			1   left  
*			2         
*			3         
*			4         
*			5         
*			6         
*			7         
*			8         
*			9         
*			10  right
*		   -99  Missing

gen ideology = -99

*marital
*			1   Married
*			2   Not Married
*		  -99   Missing

gen marital = 1 if Q1010==4|Q1010==2
replace marital = 2 if Q1010==1|Q1010==3|Q1010==5|Q1010==6|Q1010==7
replace marital = -99 if Q1010==.|Q1010==99

*urban
*			1   Urban
*			2   Rural
*		  -99   Missing

gen urban = 1 if Q13==1
replace urban = 2 if Q13==2
replace urban = -99 if Q13==.|Q13==3

*born_country
*			1   Born in country
*			0   Not born in country

gen born_country = .

keep S009 ccode country numcode country_short cow wave trust_state trust_govt trust_justice trust_police trust_civil_service trust_local_govt trust_parties trust_parliament trust_others trust_military  survey_year age yob cohort sex religion edu occupation employment_status income ideology marital urban survey born_country weight 

sort ccode wave survey_year

save "$data_dir/clean/arab8_clean.dta", replace
